package com.lkm.app_ui.help;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import com.lkm.app_ui.AppConst;
import com.lkm.app_ui.BootReceiver;
import com.lkm.app_ui.RunTimeData;

import java.io.File;

import androidx.core.content.FileProvider;


public class AppHelp {

    public static void installApk(String savePath) {
        if (RunTimeData.isXIANGCHENG) {
            RootCommand.runT("pm install -r "+savePath);


        }else{
            File file = new File(savePath);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            Context context = AppConst.getApplication();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                Uri contentUri = FileProvider.getUriForFile(context, "com.lkm.app_ui.fileprovider", file);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
            } else {
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
            }
            context.startActivity(intent);
        }
    }


    public static void rebootApp() {
        BootReceiver.startApp(AppConst.getApplication(), AppConst.getApplication().getPackageName());

        android.os.Process.killProcess(android.os.Process.myPid());
    }



    public static String getLocalVersionName() {
        String localVersion = "";
        try {
            PackageInfo packageInfo = AppConst.getApplication()
                    .getPackageManager()
                    .getPackageInfo(AppConst.getApplication().getPackageName(), 0);
            localVersion = packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return localVersion;
    }
}
